namespace CIS
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class RoleManage : Page
    {
        protected DropDownList AreaDdl;
        protected DropDownList CheckTypeDdl;
        protected CheckBox EditChk;
        protected DropDownList InfoTypeADdl;
        protected DropDownList InfoTypeBDdl;
        protected DropDownList InfoTypeCDdl;
        protected DropDownList InfoTypeDDdl;
        protected Image iRoleManage;
        protected Label lbArea;
        protected Label lbCheckAut;
        protected Label lbContent;
        protected Label lbdescript;
        protected Label lbInfoType;
        protected Label lbIsEdit;
        protected Label lbIsUMView;
        protected Label lbOperate;
        protected Label lbRoleName;
        protected Label lbSetInfoAut;
        protected Label lbTitle;
        protected Label lbUnmasterView;
        protected UserPrincipal Principal = null;
        protected TextBox RoleDescrTbx;
        protected TextBox RoleNameTbx;
        protected Repeater rptRole;
        protected Button SaveBtn;
        protected CheckBox UnmasterViewChk;

        private void AreaDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.RptRoleDataBind();
        }

        private void FillDropDownList(DropDownList tepDDL)
        {
            tepDDL.Items.Clear();
            ListItem listItem1 = new ListItem();
            listItem1.Text = "浏览";
            listItem1.Value = "0";
            ListItem listItem2 = new ListItem();
            listItem2.Text = "维护";
            listItem2.Value = "1";
            ListItem listItem3 = new ListItem();
            listItem3.Text = "禁止";
            listItem3.Value = "2";
            tepDDL.Items.Add(listItem1);
            tepDDL.Items.Add(listItem2);
            tepDDL.Items.Add(listItem3);
        }

        protected string GetAuthInfo(object objRoleId)
        {
            Guid roleId = (Guid) objRoleId;
            Role role = ManageRole.GetRoleViaId(roleId);
            return ("A:" + this.GetStringValue(role.TypeA) + " B:" + this.GetStringValue(role.TypeB) + " C:" + this.GetStringValue(role.TypeC) + " D:" + this.GetStringValue(role.TypeD));
        }

        private string GetStringValue(int i)
        {
            if (i == 0)
            {
                return "浏览";
            }
            if (i == 1)
            {
                return "编辑";
            }
            if (i == 2)
            {
                return "禁止";
            }
            return "";
        }

        protected string GetStrInSetFile(string str)
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            return ManageSystemConfig.GetSystemSetLanguage("RoleManage", str, language, path);
        }

        private void InitializeComponent()
        {
            this.AreaDdl.SelectedIndexChanged += new EventHandler(this.AreaDdl_SelectedIndexChanged);
            this.rptRole.ItemCommand += new RepeaterCommandEventHandler(this.rptRole_ItemCommand);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            this.AreaDdl.Items.Clear();
            for (int i = 0; i < area.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = area[i].AreaName;
                tmpListItem.Value = area[i].AreaId.ToString();
                this.AreaDdl.Items.Add(tmpListItem);
            }
            this.SetFrom();
            this.RptRoleDataBind();
            this.FillDropDownList(this.InfoTypeADdl);
            this.FillDropDownList(this.InfoTypeBDdl);
            this.FillDropDownList(this.InfoTypeCDdl);
            this.FillDropDownList(this.InfoTypeDDdl);
            this.FillDropDownList(this.CheckTypeDdl);
            this.EditChk.Enabled = false;
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.InitPage();
                this.SetFromImage();
            }
        }

        private void rptRole_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string cmd = e.CommandName;
            string strTmp = (string) e.CommandArgument;
            Guid roleId = new Guid(strTmp);
            switch (cmd)
            {
                case "delete":
                    if ((roleId.ToString() != "00000000-0000-0000-0000-000000000000") && (roleId.ToString() != "00000000-0000-0000-0000-000000000001"))
                    {
                        if (!ManageRole.IsExistRoleInOtherTable(roleId))
                        {
                            ManageRole.RemoveRoleViaId(roleId);
                        }
                        else
                        {
                            base.Response.Write("<script> alert('还有相关信息未被删除！') </script>");
                        }
                        this.InitPage();
                    }
                    break;

                case "modify":
                {
                    Role role = ManageRole.GetRoleViaId(roleId);
                    if (role != null)
                    {
                        this.RoleNameTbx.Text = role.RoleName;
                        this.RoleDescrTbx.Text = role.RoleDescr;
                        if (role.IsUnmasterView == 1)
                        {
                            this.UnmasterViewChk.Checked = true;
                        }
                        this.SetInfoTypeDdl(this.InfoTypeADdl, role.TypeA.ToString());
                        this.SetInfoTypeDdl(this.InfoTypeBDdl, role.TypeB.ToString());
                        this.SetInfoTypeDdl(this.InfoTypeCDdl, role.TypeC.ToString());
                        this.SetInfoTypeDdl(this.InfoTypeDDdl, role.TypeD.ToString());
                        this.SetInfoTypeDdl(this.CheckTypeDdl, role.CheckAuthorization.ToString());
                        this.EditChk.Checked = true;
                        this.Page.RegisterHiddenField("RoleId", role.RoleId.ToString());
                    }
                    break;
                }
            }
        }

        private void RptRoleDataBind()
        {
            this.rptRole.DataSource = ManageRole.GetRole(new Guid(this.AreaDdl.SelectedValue));
            this.rptRole.DataBind();
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            if (this.EditChk.Checked)
            {
                if (this.RoleNameTbx.Text.Trim() == string.Empty)
                {
                    this.RoleNameTbx.Text = string.Empty;
                    this.RoleDescrTbx.Text = string.Empty;
                    this.EditChk.Checked = false;
                    base.Response.Write("<script> alert('title不能为空') </script>");
                    return;
                }
                Guid RoleId = new Guid(base.Request.Form["RoleId"].ToString());
                int iIsUnmasterView = 0;
                if (this.UnmasterViewChk.Checked)
                {
                    iIsUnmasterView = 1;
                }
                ManageRole.UpdateRole(RoleId, this.RoleNameTbx.Text.Trim(), this.RoleDescrTbx.Text.Trim(), this.InfoTypeADdl.SelectedItem.Value, this.InfoTypeBDdl.SelectedItem.Value, this.InfoTypeCDdl.SelectedItem.Value, this.InfoTypeDDdl.SelectedItem.Value, iIsUnmasterView, this.CheckTypeDdl.SelectedValue);
                this.RoleNameTbx.Text = string.Empty;
                this.RoleDescrTbx.Text = string.Empty;
                this.EditChk.Checked = false;
                this.UnmasterViewChk.Checked = false;
            }
            else
            {
                if (this.RoleNameTbx.Text.Trim() == string.Empty)
                {
                    base.Response.Write("<script> alert('name不能为空') </script>");
                    return;
                }
                int iIsUnmasterView = 0;
                if (this.UnmasterViewChk.Checked)
                {
                    iIsUnmasterView = 1;
                }
                ManageRole.InsertNewRole(this.RoleNameTbx.Text.Trim(), this.RoleDescrTbx.Text.Trim(), this.InfoTypeADdl.SelectedItem.Value, this.InfoTypeBDdl.SelectedItem.Value, this.InfoTypeCDdl.SelectedItem.Value, this.InfoTypeDDdl.SelectedItem.Value, new Guid(this.AreaDdl.SelectedValue), iIsUnmasterView, this.CheckTypeDdl.SelectedValue);
                this.RoleNameTbx.Text = string.Empty;
                this.RoleDescrTbx.Text = string.Empty;
                this.EditChk.Checked = false;
                this.UnmasterViewChk.Checked = false;
            }
            this.RptRoleDataBind();
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            this.lbIsEdit.Text = ManageSystemConfig.GetSystemSetLanguage("RoleManage", "lbIsEdit", language, path);
            this.lbSetInfoAut.Text = ManageSystemConfig.GetSystemSetLanguage("RoleManage", "lbSetInfoAut", language, path);
            this.lbTitle.Text = ManageSystemConfig.GetSystemSetLanguage("RoleManage", "lbTitle", language, path);
            this.lbContent.Text = ManageSystemConfig.GetSystemSetLanguage("RoleManage", "lbContent", language, path);
            this.lbInfoType.Text = ManageSystemConfig.GetSystemSetLanguage("RoleManage", "lbInfoType", language, path);
            this.lbOperate.Text = ManageSystemConfig.GetSystemSetLanguage("RoleManage", "lbOperate", language, path);
            this.lbRoleName.Text = ManageSystemConfig.GetSystemSetLanguage("RoleManage", "lbRoleName", language, path);
            this.lbdescript.Text = ManageSystemConfig.GetSystemSetLanguage("RoleManage", "lbdescript", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("RoleManage", "SaveBtn", language, path);
            this.lbUnmasterView.Text = ManageSystemConfig.GetSystemSetLanguage("RoleManage", "lbUnmasterView", language, path);
            this.lbIsUMView.Text = ManageSystemConfig.GetSystemSetLanguage("RoleManage", "lbIsUMView", language, path);
            this.lbCheckAut.Text = ManageSystemConfig.GetSystemSetLanguage("RoleManage", "lbCheckAut", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iRoleManage.ImageUrl = "images/title_RoleManage.jpg";
                    break;

                case "en":
                    this.iRoleManage.ImageUrl = "images/title_RoleManageEn.jpg";
                    break;
            }
        }

        private void SetInfoTypeDdl(DropDownList tepDDL, string strValue)
        {
            for (int i = 0; i < tepDDL.Items.Count; i++)
            {
                if (tepDDL.Items[i].Value == strValue)
                {
                    tepDDL.SelectedIndex = i;
                    break;
                }
            }
        }
    }
}

